import os, sys
import gobject
if os.path.isfile('/usr/bin/gnome-session'):
    import file_mimetypeGNOME as mime
else:
    import mime_typeNonGNOME as mime
from threading import Thread
from ftplib import FTP

class AbsynthModFTP(gobject.GObject):
    __gsignals__ = {
                "dirchanged" :  (gobject.SIGNAL_RUN_LAST,
                gobject.TYPE_NONE,
                ())}
    def __init__(self):
        gobject.GObject.__init__(self)
        self.ftp = FTP()
        self.status = False
    def connect_to(self, server, user, password):
        self.ftp.connect(server)
        self.ftp.login(user, password)
        self.status = True    
    def close(self):
        self.ftp.quit()
        self.status = False
    def cwd(self, path):
        self.ftp.cwd(path)
        self.emit("dirchanged")
    def pwd(self):
        return self.ftp.pwd()
    def delete(self, file):
        self.ftp.delete(file)
        self.emit("dirchanged")
    def welcome(self):
        return self.ftp.getwelcome()
    def upload(self, filename):
        self.ftp.storbinary("STOR " + filename, file(filename, "rb"))
    def download(self, file):
        self.file = open(file, "wb")
        self.ftp.retrbinary('RETR' + file, self.ftp_file_printer_callback) 
        self.file.close()
    def ftp_file_printer_callback(self, data):
        self.file.write(data)
    def get_status(self):
        return self.status
    def list_file(self):
        list_raw = []
        list_file = []
        self.ftp.retrlines('LIST', list_raw.append)
        for item in list_raw:
            words = str(item).split()
            name = words[-1]
            size = words[4]
            mtime = str(words[5] + ' ' + words[6] + ' ' + words[7]) 
            if words[0][0] != '-':
                txt_type = "directory"
                type = mime.return_folder_mime()
                list_file.append([ txt_type, type, name, size, mtime ])
            else:
                txt_type = "file"
                type = mime.return_mime(name)   
                list_file.append([txt_type, type, name, size, mtime])
        list_file.sort()
        return list_file

class Download(Thread):
        def __init__(self, file, path, host, user, password):
            Thread.__init__(self)
            self.ftp = FTP()
            self.file = file
            self.path = path.get_text()
            self.host = host.get_text()
            self.user = user.get_text()
            self.password = password.get_text()

            self.received = 0
            
        def run(self):
            self.ftp.connect(self.host)
            self.ftp.login(self.user, self.password)
            self.ftp.cwd(self.path)
            self.size = self.ftp.size(self.file)
            self.file1 = open(self.file, "wb")
            self.ftp.retrbinary('RETR ' + self.file, self.callback) 
            self.file1.close()
            self.ftp.quit()
        
        def callback(self, data):
            self.file1.write(data)
            self.received += len(data)
            print '%.3f%% \n' % (100*self.received/self.size) 

class Upload(Thread):
        def __init__(self, file, path, host, user, password):
            Thread.__init__(self)
            self.ftp = FTP()
            self.filename = file
            self.path = path.get_text()
            self.host = host.get_text()
            self.user = user.get_text()
            self.password = password.get_text()
            
        def run(self):
            self.ftp.connect(self.host)
            self.ftp.login(self.user, self.password)
            self.ftp.cwd(self.path)
            self.ftp.storbinary("STOR " + self.filename, file(self.filename, "rb"))
            print "Completato"
            self.ftp.quit()